$(function() {
    var a = $("#cartBody");	
    var c = $("#divNone");
    var b = function() {
        var o = "";
        var h = $("#divTopMoney");
        var g = $("#divBtmMoney");
        var e = function(t, s, r, q) {
            $.PageDialog.fail(t, s, r, q);
        };
        var n = function(s, r, q) {
            $.PageDialog.confirm(s, r, q);
        };
        if (h.length > 0) {
            h.children("a").click(function() {
              //付款页面
              if(!getUserinfo()){
                  //$.PageDialog.fail('请登录用户');
				  localData.remove('reload');
                  randomSwitchBtn('login',api);
				  return false;
              }
              api.openWin({
                  name : 'team_payment',
                  url : './team_payment.html',
                  reload : true,
                  delay : 2,
              });
            });
        }
        g.children("a").click(function() {		
              //付款页面
              if(!getUserinfo()){
                  //$.PageDialog.fail('请登录用户');
				  localData.remove('reload');
                  randomSwitchBtn('login',api);
				  return false;
              }
              api.openWin({
                  name : 'team_payment',
                  url : './team_payment.html',
                  reload : true,
                  delay : 2,
              });
        });
      var team_cart = localData.get('team_cart');
      if(!team_cart){
          $('#divNone').show();
          return;
      }
      
      apiready = function(){
          
        setInterval(function(){
            if(!localData.get('team_cart_num')){
                $('.g-Cart-list').hide();
                $('#divNone').show();
            }
        },1000);
        var func = 'funcC();';
        api.execScript({
            name:'root',
            script:func
        });
        
        var teamclearCart = document.getElementById('teamclearCart');
        teamclearCart.onclick = function(){
            api.confirm({
                title: '提示',
                msg: '是否清空购物车？',
                buttons: ['确定', '取消']
            },function( ret, err ){
                if( ret ){
                    if(ret['buttonIndex'] == 1){
                        localData.remove('team_cart_num');
                        localData.remove('team_cart');
                        $('.g-Cart-list').hide();
                        $('#divNone').show();
                    }else{
                        //alert(JSON.stringify( err ));
                    }
                }else{
                     //alert( JSON.stringify( err ) );
                }
            });
        };
          
        if(api.connectionType == 'none'){
            api.showProgress({
                 title : '未连接网络',
                 text : ''
             });
            return false;
        }else{
            api.showProgress({title:'加载中',text:''});
            setTimeout(function(){
                api.hideProgress();
            },15000);
        } 
      };
        
        PostData(config.url+'/app.php?act=teamcartlist',{cart:team_cart},function(data){
            api.hideProgress();
            if(data['shop'] == 1){
                  var html = '';
                  var countCart = 0;
                  var countPrice = 0;
                  for(var i in data['shoplist']){
                         countCart++;
                         countPrice += data['shoplist'][i]['yunjiage']*data['shoplist'][i]['cart_gorenci'];
                         html += '<li><a class="fl u-Cart-img" href="javascript:openItem(\'team_item\','+data['shoplist'][i]['id']+',function(id){localData.set(\'team_goods_id\',id);});">';
                         html += '<img class="lazy" data-original="'+config.upload_url+'/'+data['shoplist'][i]['thumb']+'" border="0" alt=""/></a><div class="u-Cart-r">';
                         html += '<p class="z-Cart-tt"><a href="javascript:openItem(\'team_item\','+data['shoplist'][i]['id']+',function(id){localData.set(\'team_goods_id\',id);});" class="gray6">'+data['shoplist'][i]['title']+'</a></p>';
                         html += '<span class="gray9">组团名称：'+data['shoplist'][i]['group_name']+'</span>';
                         html += '<ins class="z-promo gray9">剩余<em class="arial">'+data['shoplist'][i]['shenyurenshu']+'</em>人次</ins>';
                         html += '<p class="gray9">总共抢购：<em class="arial">'+data['shoplist'][i]['cart_gorenci']+'</em>人次/<em class="orange arial">￥'+data['shoplist'][i]['cart_xiaoji']+'</em></p>';
                         html += '<p class="f-Cart-Other">';
                         html += '<a href="javascript:;" class="fr z-del delgoods" name="delLink" cid="'+data['shoplist'][i]['id']+'"></a>';
                         if(data['shoplist'][i]['cart_gorenci'] == 1){
                             html += '<a href="javascript:;" class="fl z-jian z-jiandis">-</a>';
                         }else{
                             html += '<a href="javascript:;" class="fl z-jian">-</a>';
                         }
                         html += '<input id="txtNum'+data['shoplist'][i]['id']+'" name="num" data-money="'+data['shoplist'][i]['yunjiage']+'" type="text" maxlength="7" value="'+data['shoplist'][i]['cart_gorenci']+'" class="fl z-amount" style="height:28px;" />';
                         if(data['shoplist'][i]['cart_gorenci'] == data['shoplist'][i]['zongrenshu']){
                             html += '<a href="javascript:;" class="fl z-jia z-jiadis">+</a>';
                         }else{
                             html += '<a href="javascript:;" class="fl z-jia">+</a>';
                         }
                         html += '<input type="hidden" value="'+data['shoplist'][i]['cart_gorenci']+'" />';
                         html += '<input type="hidden" value="'+data['shoplist'][i]['shenyurenshu']+'" />';
                         html += '</p></div></li>';
                 }
                  $('#cartBody').append(html);
                  $('.countCart').text(countCart);
                  $('.countPrice').text(countPrice+'.00');
                  $('.g-Cart-list').show();
                  lyzimg();
                  
             $("a[name=delLink]", a).each(function(q) {
                 $(this).bind("click",function() {
                     var r = $(this);
                     var t = r.attr("cid");
                     var s = function() {
                         if (team_deleteCart(t)) {
                                 r.parent().parent().parent().remove();
                                 m();
                                 f();
                             } else {
                                 e("删除失败，请重试");
                             }
                         //GetJPData(Gobal.Webpath, "ajax", "delCartItem/" + t, u);
                     };
                     n("您确定要删除吗？", s);
                 });
             });
        
        var m = function() {
            var q = 0;
            var r = 0;
            $("input:text[name=num]", a).each(function(s) {
                var t = parseInt($(this).val());
                if (!isNaN(t)) {
                    r++;
                    q += t;
                }
            });
            if (r > 0) {
                if (h.length > 0) {
                    h.children("span").html(q + ".00");
                }
                g.children("p").html('总共购买<span class="orange arial z-user">' + r + '</span>个商品  合计金额：<span class="orange arial">' + q + ".00</span> 元");
            } else {
                g.remove();
            }
        };
        var d = function() {
            var z = $(this);
            var t = z.attr("id");
            var v = t.replace("txtNum", "");
            var q = z.next().next();
            var r = parseInt(z.next().next().next().val());
            var s, y, w = /^[1-9]{1}\d{0,6}$/;
            var u;
            o = t;
            var x = function() {
                if (o != t) {
                    return;
                }
                s = q.val();
                y = z.val();
                if (y != "" && s != y) {
                    var B = $(window).width();
                    var A = (B) / 2 - z.offset().left - 127;
                    if (w.test(y)) {
                        u = parseInt(y);
                        if (u <= r) {
                            q.val(y);
                        } else {
                            u = r;
                            e("最多" + u + "人次", z, -75, A);
                            z.val(u);
                            q.val(u);
                        }
                        p(u, z);
                         
                        j(z, v, u);
                        i(z, u, r);
                        m();
                    } else {
                        e("只能输正整数哦", z, -75, A);
                        z.val(s);
                    }
                }
                setTimeout(x, 200);
            };
            x();
        };
        var p = function(r, u) {
            var t = u.parent().parent().parent();
            var q = t.find("div.z-Cart-tips");
            if (r > 100) {
                if (q.length == 0) {
                    var s = $('<div class="z-Cart-tips">已超过100人次，请谨慎参与！</div>');
                    t.prepend(s);
                }
            } else {
                q.remove();
            }
        };
        var l = function() {
            var q = $(this);
            if (o == q.attr("id")) {
                o = "";
            }
            if (q.val() == "") {
                q.val(q.next().next().val());
            }
        };
        var j = function(q, t, r) {
         
            var s = function(w) {
                if (w.code == 1) {
                    var v = $(window).width();
                    var u = (v) / 2 - q.offset().left - 127;
                    e("本期商品已购买光了", q, -75, u);
                } else {
                    if (w.code == 0) {
                        q.parent().prev().html('总共购买：<em class="arial">' + r + '</em>人次/<em class="orange arial">￥' + r + ".00</em>");
                    }
                        var money = parseInt(q.attr('data-money'));
                        if(!isNaN(money)){
                             team_addCart(t,money,r,'','cart');
                         }
                }
            };
            GetJPData(config.url+"/app.php?act=team_addshopcart", {goods_id:t},s);
        };
        var k = function(w, v) {
            var u = v.attr("id");
            var s = u.replace("txtNum", "");
            var r = parseInt(v.next().next().next().val());
            var q = v.next().next();
            var t = parseInt(q.val()) + w;
            if (t > 0 && t <= r) {
                i(v, t, r);
                q.val(t);
                v.val(t);
                p(t, v);
                j(v, s, t);
                m();
            }
        };
        var i = function(r, t, s) {
            var q = r.prev();
            var u = r.next();
            if (s == 1) {
                q.addClass("z-jiandis");
                u.addClass("z-jiadis");
            } else {
                if (t == 1) {
                    q.addClass("z-jiandis");
                    u.removeClass("z-jiadis");
                } else {
                    if (t == s) {
                        q.removeClass("z-jiandis");
                        u.addClass("z-jiadis");
                    } else {
                        q.removeClass("z-jiandis");
                        u.removeClass("z-jiadis");
                    }
                }
            }
        };
        $("input:text[name=num]", a).each(function(q) {
            var r = $(this);
            r.bind("focus", d).bind("blur", l);
            r.prev().bind("click",
            function() {
                k( - 1, r);
            });
            r.next().bind("click",
            function() {
                k(1, r);
            });
        });
        var f = function() {
            var q = $("li", "#cartBody");
            if (q.length < 1) {          
                a.parent().remove();                
                c.show();
            } else {
                if (q.length < 4) {
                    h.remove();
                }
            }
        };     
            }else{
                $('#divNone').show();
				localData.remove('team_cart');
            }
        });
    };
	 
    if (a.length > 0) {
        b();
    } else {
        c.show();
    }
});